<?php

namespace app\api\controller;

use app\api\model\project\ProjectApply as ProjectApplyModel;
use app\common\controller\Api;

use app\api\model\project\ProjectCollect as ProjectCollectModel;
use app\api\model\project\ProjectStaff as ProjectStaffModel;

/**
 * 服务人员收藏接口
 */
class Projectcollect extends Api
{

    protected $noNeedRight = ['*'];

    //收藏
    public function add()
    {
        $staff_id = input('staff_id',0);
        if (empty($staff_id)) {
            $this->error('缺少参数服务人员id');
        }
        $data = [
            'user_id' => $this->auth->id,
            'staff_id' => $staff_id,
        ];

        $collect = new ProjectCollectModel();

        $info = ProjectCollectModel::where(['staff_id'=>$staff_id, 'user_id' => $this->auth->id])->find();
        if (empty($info)){
            $res = $collect->save($data);
            if ($res){
                ProjectStaffModel::where(['id'=>$staff_id])->setInc('collect_num',1);
            }
            $this->success('收藏成功',$res);
        } else {
            $this->error('收藏失败');
        }
    }

    //删除
    public function delete()
    {
        $staff_id = input('staff_id',0);
        if (empty($staff_id)) {
            $this->error('缺少参数服务人员id');
        }

        $info = ProjectCollectModel::where(['staff_id'=>$staff_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)){
            $res = $info->delete();
            if ($res){
                ProjectStaffModel::where(['id'=>$staff_id])->setDec('collect_num',1);
            }
            $this->success('取消成功',$res);
        } else {
            $this->error('取消失败');
        }
    }

    //是否收藏
    public function iscollect()
    {
        $staff_id = input('staff_id',0);
        if (empty($staff_id)) {
            $this->error('缺少参数服务人员id');
        }

        $info = ProjectCollectModel::where(['staff_id'=>$staff_id, 'user_id' => $this->auth->id])->find();

        if (!empty($info)) {
            $this->success('',1);
        } else {
            $this->success('',0);
        }
    }

    //收藏列表
    public function lists()
    {
        $project = ProjectCollectModel::alias('pc')
            ->join('project_staff ps','pc.staff_id = ps.id')
            ->where(['pc.user_id'=>$this->auth->id])
            ->field('pc.createtime,ps.image,ps.nickname,ps.realname,ps.title,ps.collect_num')
            ->order('pc.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$project);
    }

    //收藏排名
    public function ranking()
    {
        $list = ProjectStaffModel::field('image,nickname,realname,title,collect_num')
            ->order('collect_num desc')
            ->paginate(10, false, ['query' => request()->get()]);
        $this->success('',$list);
    }

    //服务人员的收藏列表
    public function collectList()
    {
        $staff_id = input('staff_id',0);

        $list = ProjectCollectModel::alias('hc')
            ->join('user u','hc.user_id = u.id')
            ->where(['hc.user_id'=>$this->auth->id,'hc.staff_id'=>$staff_id])
            ->field('u.nickname,u.avatar,hc.createtime')
            ->order('hc.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);
    }
}